package com.prs.crm.action.oa.report;

import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.oa.Knowledge;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.oa.KnowledgeService;

public class KnowledgeReportAction extends ReportAction {

	private static final long serialVersionUID = 1516416478650724229L;
	private KnowledgeService knowledgeService;
	private ChartResult chartResult;
	private ChartService chartService;
	private PaginationSupport pagination;
	private String category;
	private int exportType;
	private Date datestart;
	private Date dateend;

	@Action("editKnowledgeReport")
	public String editKnowledgeReport() {
		return SUCCESS;
	}

	@Action(value = "viewKnowledgePie", results = {
			@Result(name = "success", type = "chain", location = "editKnowledgeReport"),
			@Result(name = "error", type = "chain", location = "editKnowledgeReport") })
	public String viewKnowledgePie() {
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		Collection<?> dataList = this.getKnowledgeService()
				.getReportKnowledgeSource(datestart, dateend);
		chartResult = this.getChartService().export(
				this.getKnowledgeService().fillDataset(dataList,
						Constrains.REPORT_PIECHART), "知识统计分析",
				"listReportKnowledge");
		return SUCCESS;
	}

	@Action(value = "viewKnowledgeBar", results = {
			@Result(name = "error", type = "chain", location = "editKnowledgeReport"),
			@Result(name = "success", type = "chain", location = "editKnowledgeReport") })
	public String viewKnowledgeBar() {
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		Collection<?> dataList = this.getKnowledgeService()
				.getReportKnowledgeSource(datestart, dateend);
		chartResult = this.getChartService().export(
				this.getKnowledgeService().fillDataset(dataList,
						Constrains.REPORT_BARCHART), "知识统计分析", "数量", "",
				"listReportKnowledge");
		return SUCCESS;
	}

	@Action("listReportKnowledge")
	public String listReportKnowledge() {
		category = ReportUtils.URLDecode(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Knowledge.class);
		if (datestart != null)
			criteria.add(Restrictions.ge("recordTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("recordTime", dateend));
		if (category != null && category.trim().length() > 0) {
			criteria.createAlias("type", "t");
			criteria.add(Restrictions.eq("t.name", category));
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getKnowledgeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportKnowledgeReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportKnowledgeReport() {
		ReportTable table = new ReportTable("导出知识统计分析报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("标题", "title"));
		table.addCell(new ReportCell("类型", "type.name"));
		table.addCell(new ReportCell("收录人", "recorder.resume.name"));
		table.addCell(new ReportCell("收录时间", "creattime"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Knowledge.class);
		if (datestart != null)
			criteria.add(Restrictions.ge("recordTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("recordTime", dateend));
		if (category != null && category.trim().length() > 0) {
			criteria.createAlias("type", "t");
			criteria.add(Restrictions.eq("t.name", category));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	public KnowledgeService getKnowledgeService() {
		return knowledgeService;
	}

	public void setKnowledgeService(KnowledgeService knowledgeService) {
		this.knowledgeService = knowledgeService;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public int getExportType() {
		return exportType;
	}

	public void setExportType(int exportType) {
		this.exportType = exportType;
	}

	public Date getDatestart() {
		return datestart;
	}

	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}

	public Date getDateend() {
		return dateend;
	}

	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}

}
